package com.sd.cti.resp;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.Kv;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.sd.common.utils.DateProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
 * 客户端管理类
 */
@Component
public class CustomerManager {
    /**
     * 日志
     */
    private static final Logger log = LoggerFactory.getLogger(CustomerManager.class);

    @Value("${picc.customer.serverUrl}")
    private String customerUrl;
    @Autowired
    private DateProvider dateProvider;
    /**
     * 通知登录结果
     */
    public void respLogin(String agentId,String extNum,String tokenId){
        try {
            Engine engine = Engine.use();
            //engine模版渲染
            engine.setDevMode(true);
            engine.setToClassPathSourceFactory();
            Template template = engine.getTemplate("/resp/respLogin.xml");
            Kv kv = Kv.by("agentId", agentId);
            kv.put("station",extNum);
            kv.put("optionTime",dateProvider.formatDatetime(new Date()));
            kv.put("permission",tokenId);
            String xmlString = template.renderToString(kv);
            log.info("respLogin:[{}]",xmlString);
            HttpKit.post(customerUrl,xmlString);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 坐席登出
     * @param agentId 坐席id
     * @param extNum 分机号
     * @param tokenId tokenId
     */
    public void respLogout(String agentId,String extNum,String tokenId){
        try {
            Engine engine = Engine.use();
            //engine模版渲染
            engine.setDevMode(true);
            engine.setToClassPathSourceFactory();
            Template template = engine.getTemplate("/resp/respLogout.xml");
            Kv kv = Kv.by("agentId", agentId);
            kv.put("station",extNum);
            kv.put("optionTime",dateProvider.formatDatetime(new Date()));
            kv.put("permission",tokenId);
            String xmlString = template.renderToString(kv);
            log.info("respLogin:[{}]",xmlString);
            HttpKit.post(customerUrl,xmlString);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
